package com.example.shoppings.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.shoppings.R;
import com.example.shoppings.activity.ProductDetailActivity;
import com.example.shoppings.sql.Products;
import com.example.shoppings.utils.GlideImageLoader;
import com.youth.banner.Banner;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hasee on 2017-05-12.
 */

public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.MyHolder>{

    private Context context;
    private List<Products> productses;
    private List<Integer> images=new ArrayList<>();
    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener{
        public void OnIitemClick();
    }

    public ProductsAdapter(Context context,List<Products> productses) {
        this.context=context;
        this.productses=productses;
        images.add(R.mipmap.main1);
        images.add(R.mipmap.main2);
        images.add(R.mipmap.main3);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyHolder(LayoutInflater.from(context).inflate(R.layout.item_mainrecyclerview,parent,false));
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        final Products products=productses.get(position);
        holder.banner.setImages(products.getImgMainPath())
        .setImageLoader(new GlideImageLoader()).start();
        holder.tvName.setText(products.getProName());
        holder.tvPrice.setText("￥："+products.getProPrice());
        holder.tvMsg.setText(products.getProMsg());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, ProductDetailActivity.class);
                Bundle b=new Bundle();
                b.putSerializable("product", (Serializable) products);
                intent.putExtras(b);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return productses!=null?productses.size():0;
    }

    class MyHolder extends RecyclerView.ViewHolder{
        private Banner banner;
        private TextView tvName,tvMsg,tvPrice;

        public MyHolder(View itemView) {
            super(itemView);
            banner= (Banner) itemView.findViewById(R.id.item_vp);
            tvName= (TextView) itemView.findViewById(R.id.tv_proName);
            tvPrice= (TextView) itemView.findViewById(R.id.tv_price);
            tvMsg= (TextView) itemView.findViewById(R.id.tv_msg);
        }
    }
}
